using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace MonoCash.ObjectModel
{
    class TransactionDetailCollection : System.ComponentModel.BindingList<TransactionDetail>
    {
        private Account Account;
        public TransactionDetailCollection(Account account)
            : base()
        {
            Account = account;

            foreach (DataRow row in Program.AppSettings.Data.TransactionDetails.Select("AccountID=" + Account.ID))
            {
                base.Add(new TransactionDetail(row["ID"]));
            }

        }
        //protected override void OnListChanged(System.ComponentModel.ListChangedEventArgs e)
        //{
        //    if (this.Count > 1)
        //    {
        //        List<TransactionDetail> toDelete = new List<TransactionDetail>();
        //        foreach (TransactionDetail td in this)
        //        {
        //            if (td.Amount == 0 && td.Quantity == 0)
        //            {
        //                td.Delete();
        //            }
        //        }
        //    }
        //    base.OnListChanged(e);
        //}
        protected override void ClearItems()
        {
            foreach (TransactionDetail td in base.Items)
            {
                td.Delete();
            }
            base.ClearItems();
        }

        protected override void RemoveItem(int index)
        {
            base[index].Delete();
            base.RemoveItem(index);
        }
        protected override void OnAddingNew(System.ComponentModel.AddingNewEventArgs e)
        {
            TransactionDetail td = new TransactionDetail();
            td.AccountID = Account.ID;
            e.NewObject = td;
            base.OnAddingNew(e);
        }
        public new void Add(TransactionDetail newDetail)
        {
            newDetail.AccountID = Account.ID;
            base.Add(newDetail);
        }
        public new void Remove(TransactionDetail item)
        {
            base.Remove(item);
            item.Delete();
        }

    }
}
